package io.renren.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.HttpUtils;
import io.renren.util.MD5;
import io.renren.util.RandomUtils;
import io.renren.util.SignUtils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lizhi
 * @Date:2019/8/13 15:13
 */
public class GsCreateOrder {


    public static void main(String[] args) throws Exception{

        String apiurl = "http://39.99.254.86:8030/api/gateway/create";

       HashMap<String, String> reqdata = new HashMap<String, String>();

        //支付宝
        reqdata.put("mch_id", "1000266");
        reqdata.put("child_type", "H5");
        reqdata.put("out_trade_no", RandomUtils.generateLowerString(15));
        reqdata.put("pay_type", "PERSONAL_RED_PACK");
        reqdata.put("notify_url", URLEncoder.encode("http://dad.natapp4.cc/api/jhcallback", "utf-8"));
        reqdata.put("total_fee", "1.0");
        reqdata.put("timestamp", ""+System.currentTimeMillis()/1000);
//child_type=H5&mch_id=11111111&mch_secret=CnU5LmMlHNKrxV5ZBmlnblkQgRKaAgDz
//	&notify_url=http%3A%2F%2Fziyouzhifu.cn%2FPay_Yonghe_notifyurl.html
//	&out_trade_no=20200101134340995610&pay_type=PERSONAL_RED_PACK
//	&timestamp=1577857420&total_fee=100.00
        reqdata.put("mch_secret","bCGL8rSCIqeDyg2yhkhTZu5pjKBGOs7x");
        StringBuilder buf = new StringBuilder((reqdata.size() +1) * 10);
        SignUtils.buildPayParams(buf,reqdata,false);
        String preStr = buf.toString();
        System.out.println("---请求--"+preStr);



        String sign = MD5.getMD5(preStr).toUpperCase();
        reqdata.put("sign",sign);
        reqdata.remove("mch_secret");
        System.out.println(reqdata);
        JSONObject reqobj=new JSONObject();
        for(Map.Entry<String, String> entry : reqdata.entrySet()){
            String mapKey = entry.getKey();
            String mapValue = entry.getValue();

            reqobj.put(mapKey,mapValue);
        }
        System.out.println(reqobj.toJSONString());
       String payurl=  HttpUtils.postJson(apiurl, reqobj.toJSONString() );
   //     String payurl = result.getBody();
        System.out.println(payurl);
        JSONObject resobj= JSON.parseObject(payurl);

        Integer code = resobj.getInteger("code");
        String msg = resobj.getString("msg");
        System.err.println("返回二维码信息:"+msg);
        if(code==100){
            JSONObject dataobj=resobj.getJSONObject("data");
            String payUrl = dataobj.getString("url");
            System.out.println("payUrl---"+payUrl);
        }
//{"status":true,"code":100,"data":{"order_id":"20200201183117141393","url":"http://47.104.87.76:8020/pay/gateway/20200201183117141393"}}

    }



}
